package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class NewsListFragment extends Fragment {
    private List<News> newsList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_list, container, false);

        RecyclerView recyclerView = view.findViewById(R.id.recycler_view_news);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        NewsAdapter adapter = new NewsAdapter(newsList);
        adapter.setOnItemClickListener(news -> {
            // 启动 NewsDetailActivity 并传递新闻数据
            Intent intent = new Intent(getActivity(), NewsDetailActivity.class);
            intent.putExtra(NewsDetailActivity.EXTRA_NEWS, news);
            startActivity(intent);
        });
        recyclerView.setAdapter(adapter);

        // 初始化新闻数据
        initializeNewsData();

        return view;
    }

    private void initializeNewsData() {
        newsList.add(new News("新闻一", "池明哲", "这是第一条新闻的详细内容"));
        newsList.add(new News("新闻二", "池明哲", "这是第二条新闻的详细内容"));
        newsList.add(new News("新闻三", "池明哲", "这是第三条新闻的详细内容"));
    }
}